package net.hctek.AnnouncerStation.util.file;

import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

import net.hctek.AnnouncerStation.bean.User;
import net.hctek.AnnouncerStation.service.IdentityServiceI;
import net.hctek.AnnouncerStation.util.Contant;
import net.hctek.AnnouncerStation.util.CookieUtils;



@SuppressWarnings("unused")
public class LoginInterceptor extends AbstractInterceptor {
	
	private static final long serialVersionUID = 1L;

	@Autowired
	private IdentityServiceI identityService;
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		
		System.out.println("经过拦截器");
		ServletActionContext.getRequest().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute(Contant.SESSION_USER);
		if(user==null){
			
			Cookie cookie = CookieUtils.getCookieByName(Contant.REMME);
			if(cookie!=null){
				
				String userId = cookie.getValue();
				user = identityService.getUserById(userId);
				
				ServletActionContext.getRequest().getSession().setAttribute(Contant.SESSION_USER, user);
			}
			
		}
		return invocation.invoke();
		
	}

}

